﻿using cfg.Myskill;
using UnityEngine;

public class PlayerDashState: PlayerStateBase
{
    public PlayerDashState(ControllerBase controller, StateMachineBase stateMachine, string stateName,string 
        animatorParameter) 
        : base(controller, stateMachine, stateName,animatorParameter)
    {
    }
    
    private float createCloneDelay;//复制间隔
    private DashSkill skill;
    private Dash skillInfo;
    public override void Enter()
    {
        base.Enter();
        if (skill==null)
        {
            skill = SkillManager.Instance.GetSkill<DashSkill>(Setting.curDashSkill);
            skillInfo = LoadCofigManager.Instance.GetSkillInfo<Dash>(Setting.curDashSkill);
        }

        if (Setting.curDashSkill.Equals(Setting.SkillCloneAttack)||Setting.curDashSkill.Equals(Setting.SkillCloneDash))
        {
            //复制一个自己攻击
            SkillManager.Instance.CreateClone<DashSkill>
            ("ClonePlayer",controller.transform.position,controller.faceDir,Setting.curDashSkill);
            
        }
        
    }

    public override void Update()
    {
        base.Update();
        Dash();
    }
    
    private void Dash()
    {
        if (stateDuration>skillInfo.SkillDuration)
        {
            if (controller.isGround)//地面
            {
                stateMachine.ChangeState(Setting.idle);
                return;
            }
            else
            {
                stateMachine.ChangeState(Setting.air);
                return;
            }
        }

        //冲刺残影
        /*if (Setting.curDashSkill.Equals(Setting.SkillCloneDash))
        {
            if (createCloneDelay<0)
            {
                createCloneDelay =999f;
                SkillManager.Instance.CreateClone<DashSkill>
                ("ClonePlayer",controller.transform.position,controller.faceDir,Setting.curDashSkill);
                
            }
            else
            {
                createCloneDelay -= Time.deltaTime;
            }
            
        }*/
        controller.SetVelocity(new Vector2(SelfController.dashDir*skillInfo.DashSpeed,0));
    }

    public override void Exit()
    {
        if (Setting.curDashSkill.Equals(Setting.SkillCloneDash))
        {
            //复制一个自己攻击
            SkillManager.Instance.CreateClone<DashSkill>
                ("ClonePlayer",controller.transform.position,controller.faceDir,Setting.curDashSkill);
        }
        base.Exit();
    }
}